import time
from pathlib import Path
import os
import re
import shutil
import json
import xml.etree.ElementTree as ET
from xml.dom.minidom import parse

def checkKeilAssert(uvprojx_path):
    """
    检查 USE_FULL_ASSERT 在Keil工程文件中的位置
    """
    # 加载XML文件
    tree = ET.parse(uvprojx_path)
    root = tree.getroot()
    value11 = ''

    # 获取多层节点的值
    value = root.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define').text
    print(value)
    if 'USE_FULL_ASSERT' in value:
        pass
    else:
       value11 = value+',USE_FULL_ASSERT'

    # 修改多层节点的值
    root.find('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls/Define').text = value11

    # 保存修改后的XML文件
    tree.write(uvprojx_path)

# uvprojx_path = 'HK32T010_Demo.uvprojx'
# checkKeilAssert(uvprojx_path)

def checkIarAssert(uvprojx_path):
    """
    检查 USE_FULL_ASSERT 在Keil工程文件中的位置
    """
    # 解析.ewp文件
    tree = ET.parse(ewp_path)
    root = tree.getroot()

    configuration_node = root.findall('configuration')[0]
    # 遍历子节点，并获取节点的文本值
    settings_node = configuration_node.findall('settings')
    for set_node in settings_node:
        if set_node.find('name').text == 'ICCARM':
            for data_node in set_node.findall('data'):
                for option_node in data_node.findall('option'):
                    if option_node.find('name').text == 'CCPreprocFile':
                        print(option_node.find('state').text)

    # 保存修改后的XML文件
    tree.write(uvprojx_path)

ewp_path = 'HK32T010_Demo.ewp'
# checkIarAssert(ewp_path)

def checkkeilProj(uvprojx_path):
    uvprojx_config_dict = dict()
    targetCommonOption_key = ['Device','Vendor','PackID','FlashDriverDll','SFDFile',
                                  'OutputDirectory','OutputName','CreateExecutable','CreateLib',
                                  'CreateHexFile','DebugInformation','BrowseInformation',
                                  'ListingPath','HexFormatSelection','Merge32K','CreateBatchFile']

    tree = ET.parse(uvprojx_path)
    root = tree.getroot()

    # param_list = ['FileName','FileType','FilePath']
    # Group_node = root.findall('Targets/Target/Groups/Group')
    # for std_Group_node in Group_node:
    #     if std_Group_node.find('GroupName').text == 'Doc':
    #         for param in param_list:
    #             value11 = std_Group_node.find(f'Files/File/{param}').text
    #             uvprojx_config_dict.setdefault(param,value11)

    TargetCommonOption_node = root.findall('Targets/Target/TargetOption/TargetCommonOption')
    for set_TargetCommonOption_node in TargetCommonOption_node:
        for param in targetCommonOption_key:
            value11 = set_TargetCommonOption_node.find(f'{param}').text
            uvprojx_config_dict.setdefault(param,value11)

    # 节点Targets,Target,TargetOption,TargetArmAds,Cads,VariousControls下
    VariousControls_node = root.findall('Targets/Target/TargetOption/TargetArmAds/Cads/VariousControls')
    for std_VariousControls_node in VariousControls_node:
        param_value = std_VariousControls_node.find('IncludePath').text
        uvprojx_config_dict.setdefault('IncludePath',param_value)
    
    print(f'uvprojx_config_dict---{uvprojx_config_dict}')
    # root = parse(uvprojx_path).documentElement
    # first_node = root.getElementsByTagName('Targets')

    # for node in first_node:
    #     # 获取第二层节点
    #     second_layer = node.getElementsByTagName('Target')
    #     for sub_sub_node in second_layer:
    #         # 检查节点Targets,Target,Groups,Group
    #         Groups_node = sub_sub_node.getElementsByTagName('Groups')
    #         for groups_node in Groups_node:
    #             group_node = groups_node.getElementsByTagName('Group')
    #             for param_node in group_node:
    #                 groupName = param_node.getElementsByTagName('GroupName')[0].firstChild.data
    #                 if groupName == 'Doc':
    #                     groupName_Files_node = param_node.getElementsByTagName('Files')[0]
    #                     groupName_File_node = groupName_Files_node.getElementsByTagName('File')
    #                     aa=groupName_File_node[0].getElementsByTagName('FileName')[0].firstChild.data
    #                     print(aa)
    #                     # for file_node in groupName_File_node:
                        #     fileName = file_node.getElementsByTagName('FileName')[0].firstChild.data
                        #     fileType = file_node.getElementsByTagName('FileType')[0].firstChild.data
                        #     filePath = file_node.getElementsByTagName('FilePath')[0].firstChild.data
                        #     uvprojx_config_dict.setdefault('FileName',fileName)

uvprojx_path = 'hk32c030xx_Demo.uvprojx'
# checkkeilProj(uvprojx_path)


def checkIarProj(ewp1_path):

    ewp_config_dict = dict()
    option_name_list = ['IlinkIcfFile']

    tree = ET.parse(ewp1_path)
    root = tree.getroot()

    # 获取option对象， GFPUDeviceSlave
    configuration_nodes = root.findall('configuration')
    
    for configuration_nodes_child in configuration_nodes:
        debug_value = configuration_nodes_child.find('debug').text
        if debug_value == '1':
            option_nodes = configuration_nodes_child.findall('settings/data/option')
            # 遍历子节点，并获取节点的文本值
            for option_nodes_obj in option_nodes:
                option_name = option_nodes_obj.find('name').text
                if option_name in option_name_list:
                    for state_nodes_obj in option_nodes_obj:
                        option_state = state_nodes_obj.find('state').text
                        ewp_config_dict.setdefault(option_name,[]).append(option_state)

    print(f'ewp_config_dict---{ewp_config_dict}')

ewp1_path = 'HK32T010_Demo.ewp'
# checkIarProj(ewp1_path)


def myparsexml(xml_path):
    # 加载XML文件
    tree = ET.parse(xml_path)
    noedroot = tree.getroot()
    xml_data_dict = {}

    # 获取软件更新内容
    msgver_list = list()
    tag_messages = noedroot.findall('messages')
    for msgver in tag_messages:
        message_name = msgver.find('message').text
        if message_name:
            msgver_list.append(message_name)
    xml_data_dict.setdefault('msgver',msgver_list)

    # 获取资源文件内容
    msgdata_list = list()
    tag_msgdata = noedroot.findall('msgdata')
    for msgdata in tag_msgdata:
        message_name = msgdata.find('message').text
        if message_name:
            msgdata_list.append(message_name)
    xml_data_dict.setdefault('msgdata',msgdata_list)

    # 获取资源文件目录
    rootSourceFile_list = list()
    tag_rootSourceFile = noedroot.findall('rootSourceFiles')
    for src_file in tag_rootSourceFile:
        src_file_name = src_file.find('rootSourceFile').text
        if src_file_name:
            rootSourceFile_list.append(src_file_name)
    xml_data_dict.setdefault('source_path',rootSourceFile_list)

    # 获取资源文件与版本信息
    files_list = list()
    tag_files = noedroot.findall('files/file')
    for file_node in tag_files:
        file_name = file_node.find('name').text
        file_ver = file_node.find('ver').text
        files_list.append({'name':file_name,'ver':file_ver})
    xml_data_dict.setdefault('file_ver',files_list)

    print(xml_data_dict)
    if xml_data_dict.get('msgver'):
        print(11111)
xml_path = 'Updater.xml'
myparsexml(xml_path)